package com.example.bilibili;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.Window;
import android.widget.RadioGroup;

import java.util.ArrayList;

public class WelcomeActivity extends AppCompatActivity {

    private int position = 0;
    private ArrayList<Fragment> fragments;
    private Fragment tempFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_welcome);
        initFragment();
        initListener();
    }

    private void initListener() {
        //RadioGroup:单选
        RadioGroup rg_main = findViewById(R.id.rg_main);
        //监听单选按钮变化
        rg_main.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.rb_channel:
                        position = 1;
                        break;
                    case R.id.rb_dynamic:
                        position = 2;
                        break;
                    case R.id.rb_vip:
                        position = 3;
                        break;
                    case R.id.rb_mine:
                        position = 4;
                        break;
                    default://为首页以及默认时
                        position = 0;
                        break;
                }
                //获取当前的fragment
                Fragment baseFragment = getFragment(position);
                //实现fragment的跳转
                switchFragment(tempFragment,baseFragment);
            }
        });
        rg_main.check(R.id.rb_home);
    }

    private void switchFragment(Fragment fromFragment, Fragment nextFragment) {
        if (tempFragment != nextFragment) {
            tempFragment = nextFragment;
            if (nextFragment != null) {
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                //判断nextFragment是否添加
                if (!nextFragment.isAdded()) {
                    //隐藏当前Fragment
                    if (fromFragment != null) {
                        transaction.hide(fromFragment);
                    }
                    //添加Fragment
                    transaction.add(R.id.frameLayout, nextFragment).commit();
                } else {
                    //隐藏当前Fragment
                    if (fromFragment != null) {
                        transaction.hide(fromFragment);
                    }
                    transaction.show(nextFragment).commit();
                }
            }
        }
    }

    private Fragment getFragment(int position) {
        if(fragments!=null && fragments.size()>0){
            return fragments.get(position);
        }
        return null;
    }

    private void initFragment() {
        fragments = new ArrayList<>();
        fragments.add(new HomeFragment());
        fragments.add(new ChannelFragment());
        fragments.add(new DynamicFragment());
        fragments.add(new VipFragment());
        fragments.add(new MineFragment());
    }
}